package virtualstock.login;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AdminLoginServlet
 */
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";
	
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//get parameters from the html
    	String userName = (String)request.getParameter("username");
    	String password = (String)request.getParameter("password");
    	
    	if(userName.equals(AdminLoginServlet.USERNAME) && password.equals(AdminLoginServlet.PASSWORD)) {
    		//successful login
    		request.getSession().setAttribute("adminlogin", "true");
    		request.getRequestDispatcher("adminHomePage.jsp").forward(request, response);
    	}
    	else {
    		//response.sendRedirect("error\\errorPassword.html");
    		request.getRequestDispatcher("index.html").forward(request, response);
    	}
    	
    }
    
    
    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			processRequest(request, response);

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

			processRequest(request, response);
		
	}

}
